package com.jason.choosenamefx.service;

import com.jason.choosenamefx.dao.SqliteHelper;
import com.jason.choosenamefx.entity.RowMapper;

import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * sql_lite 数据库测试
 */
public class SqliteTest {

    /**
     * 解决路径的问题 暂时还没有排查出 相对路径 为什为null的原因
     * @param args
     */
    public static void main2(String[] args) {
        System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));
        System.out.println(SqliteTest.class.getClassLoader().getResource(""));
        System.out.println(ClassLoader.getSystemResource(""));
        System.out.println(SqliteTest.class.getResource(""));
        System.out.println(SqliteTest.class.getResource("/")); //Class文件所在路径
        System.out.println(new File("/").getAbsolutePath());
        System.out.println(System.getProperty("user.dir"));
    }

    public static void main(String[] args) {
        try {
            SqliteHelper h = new SqliteHelper("test.db");
            h.executeUpdate("drop table if exists test;");
            h.executeUpdate("create table test(name varchar(20));");
            h.executeUpdate("insert into test values('Sql_Lite_数据库测试_ByJason');");
            List<String> sList = h.executeQuery("select name from test", new RowMapper<String>() {
                @Override
                public String mapRow(ResultSet rs, int index)
                        throws SQLException {
                    return rs.getString("name");
                }
            });
            System.out.println(sList.get(0));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
